<!doctype html>
<html> 
 <head> 
  <meta charset="utf-8"> 
  <title>Layui 模块规范</title> 
  <meta name="renderer" content="webkit"> 
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
  <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
  <meta name="apple-mobile-web-app-capable" content="yes"> 
  <meta name="format-detection" content="telephone=no"> 
  <link rel="stylesheet" href="/layui/dist/css/layui.css" media="all"> 
  <link rel="stylesheet" href="/static/css/global.css" media="all"> 
 </head> 
 <body> 
  <div class="layui-header header header-doc"> 
   <div class="layui-container"> <a class="logo" href="/index.html"> <img src="/static/images/layui/logo.png" alt="layui"> </a> 
    <div class="layui-form layui-hide-xs component" lay-filter="LAY-site-header-component"></div> 
    <div class="layui-hide-xs site-notice"></div> 
    <ul class="layui-nav" id="LAY_NAV_TOP"> 
     <li class="layui-nav-item layui-this"> <a href="/doc/index.html">文档</a> </li> 
     <li class="layui-nav-item "> <a href="/demo/index.html">示例</a> </li> 
     <li class="layui-nav-item"> <a href="javascript:;"> <!--<span class="layui-badge-dot" style="left:0; right: auto; margin: -4px 0 0 5px;"></span>--> 周边 </a> 
      <dl class="layui-nav-child layui-nav-child-c"> 
       <dd class="layui-hide-sm layui-show-xs" lay-unselect> <a href="https://gitee.com/sentsin/layui/issues" target="_blank" rel="nofollow">问题反馈</a> 
        <hr> 
       </dd> 
       <dd lay-unselect>
        <a href="/alone.html" target="_blank" lay-unselect>独立组件</a>
       </dd> 
       <dd lay-unselect>
        <a href="/extend/" target="_blank">扩展组件</a>
       </dd> 
      </dl> </li> 
     <li class="layui-nav-item layui-hide-xs"> <a href="//gitee.com/sentsin/layui/issues" target="_blank" rel="nofollow">反馈</a> </li> 
    </ul> 
   </div> 
  </div> <!-- 让IE8/9支持媒体查询，从而兼容栅格 --> <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]--> 
  <ul class="site-dir"> 
   <li><a href="#before"><cite>预先加载模块</cite></a></li> 
   <li><a href="#space"><cite>模块命名空间</cite></a></li> 
   <li><a href="#extend"><cite>扩展一个 layui 模块</cite></a></li> 
  </ul> 
  <div class="layui-container layui-row"> 
   <div class="layui-col-md3"> 
    <div class="layui-panel site-menu"> 
     <ul class="layui-menu layui-menu-lg"> 
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}"> 
       <div class="layui-menu-body-title">
         基础说明 
       </div> 
       <hr> 
       <ul> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/index.html"> <span>开始使用 </span> <span class="layui-font-12 layui-font-gray">Getting Started</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/base/infrastructure.html"> <span>底层方法 </span> <span class="layui-font-12 layui-font-gray">基础支撑</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/base/element.html"> <span>页面元素 </span> <span class="layui-font-12 layui-font-gray">规范 公共类 属性</span> </a> 
         </div> </li> 
        <li class="layui-menu-item-checked2"> 
         <div class="layui-menu-body-title"> <a href="/doc/base/modules.html"> <span>模块规范 </span> <span class="layui-font-12 layui-font-gray">使用 扩展</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/base/faq.html"> <span>常见问题 </span> <span class="layui-font-12 layui-font-gray">FAQ</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/base/changelog.html"> <span>更新日志 </span> <span class="layui-font-12 layui-font-gray">changelog</span> <span class="layui-badge-dot"></span> </a> 
         </div> </li> 
       </ul> </li> 
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}"> 
       <div class="layui-menu-body-title">
         页面元素 
       </div> 
       <hr> 
       <ul> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/layout.html"> <span>布局 </span> <span class="layui-font-12 layui-font-gray">栅格 后台管理布局</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/color.html"> <span>颜色 </span> <span class="layui-font-12 layui-font-gray">主色调 颜色搭配</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/icon.html"> <span>图标 </span> <span class="layui-font-12 layui-font-gray">iconfont 字体图标</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/anim.html"> <span>动画 </span> <span class="layui-font-12 layui-font-gray">内置 CSS3 动画</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/button.html"> <span>按钮 </span> <span class="layui-font-12 layui-font-gray">button</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/form.html"> <span>表单 </span> <span class="layui-font-12 layui-font-gray">form 元素集合</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/nav.html"> <span>导航 </span> <span class="layui-font-12 layui-font-gray">nav 面包屑</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/menu.html"> <span>菜单 </span> <span class="layui-font-12 layui-font-gray">menu 基础菜单</span> <span class="layui-badge-dot"></span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/tab.html"> <span>选项卡 </span> <span class="layui-font-12 layui-font-gray">Tabs 切换</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/progress.html"> <span>进度条 </span> <span class="layui-font-12 layui-font-gray">progress</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/panel.html"> <span>面板 </span> <span class="layui-font-12 layui-font-gray">panel 卡片 折叠</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/table.html"> <span>表格 </span> <span class="layui-font-12 layui-font-gray">静态 table</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/badge.html"> <span>徽章 </span> <span class="layui-font-12 layui-font-gray">小圆点 小边框</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/timeline.html"> <span>时间线 </span> <span class="layui-font-12 layui-font-gray">timeline</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/element/auxiliar.html"> <span>辅助 </span> <span class="layui-font-12 layui-font-gray">引用 字段集 横线等</span> </a> 
         </div> </li> 
       </ul> </li> 
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}"> 
       <div class="layui-menu-body-title">
         内置模块 
       </div> 
       <hr> 
       <ul> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/layer.html"> <span>弹出层 </span> <span class="layui-font-12 layui-font-gray">layer</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/laydate.html"> <span>日期与时间选择 </span> <span class="layui-font-12 layui-font-gray">laydate</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/laypage.html"> <span>分页 </span> <span class="layui-font-12 layui-font-gray">laypage</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/laytpl.html"> <span>模板引擎 </span> <span class="layui-font-12 layui-font-gray">laytpl</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/table.html"> <span>数据表格 </span> <span class="layui-font-12 layui-font-gray">table</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/form.html"> <span>表单 </span> <span class="layui-font-12 layui-font-gray">form</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/upload.html"> <span>文件上传 </span> <span class="layui-font-12 layui-font-gray">upload</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/dropdown.html"> <span>下拉菜单 </span> <span class="layui-font-12 layui-font-gray">dropdown</span> <span class="layui-badge-dot"></span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/transfer.html"> <span>穿梭框 </span> <span class="layui-font-12 layui-font-gray">transfer</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/tree.html"> <span>树形组件 </span> <span class="layui-font-12 layui-font-gray">tree</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/colorpicker.html"> <span>颜色选择器 </span> <span class="layui-font-12 layui-font-gray">colorpicker</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/element.html"> <span>常用元素操作 </span> <span class="layui-font-12 layui-font-gray">element</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/slider.html"> <span>滑块 </span> <span class="layui-font-12 layui-font-gray">slider</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/rate.html"> <span>评分 </span> <span class="layui-font-12 layui-font-gray">rate</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/carousel.html"> <span>轮播 </span> <span class="layui-font-12 layui-font-gray">carousel</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/flow.html"> <span>流加载 </span> <span class="layui-font-12 layui-font-gray">flow</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/util.html"> <span>工具组件 </span> <span class="layui-font-12 layui-font-gray">util</span> </a> 
         </div> </li> 
        <li class=""> 
         <div class="layui-menu-body-title"> <a href="/doc/modules/code.html"> <span>代码高亮显示 </span> <span class="layui-font-12 layui-font-gray">code</span> </a> 
         </div> </li> 
       </ul> </li> 
     </ul> 
    </div> 
    <div class="layui-hide-v">
      - 
    </div> 
   </div> 
   <div class="layui-col-md9 site-content"> 
    <h1 class="site-h1">模块规范</h1> 
    <blockquote class="layui-elem-quote">
      layui 定义了一套更轻量的模块规范。并且这种方式在经过了大量的实践后，成为 layui 最核心的模块加载引擎。 
    </blockquote> 
    <div class="site-title"> 
     <fieldset>
      <legend><a name="before">预先加载模块</a></legend>
     </fieldset> 
    </div> 
    <div class="site-text"> 
     <p>layui 通过 use 方法加载模块。当你的 JS 需要用到 layui 模块的时候，且避免到处写 layui.use() 的麻烦。你应该在最外层如此定义： </p> 
     <pre class="layui-code">layui.use(['form', 'upload'], function(){  //如果只加载一个模块，可以不填数组。如：<em>layui.use('form')</em>
  var form = layui.form //获取form模块
  ,upload = layui.upload; //获取upload模块
  
  //监听提交按钮
  form.on('submit(test)', function(data){
    console.log(data);
  });
  
  //实例化一个上传控件
  upload({
    url: '上传接口url'
    ,success: function(data){
      console.log(data);
    }
  })
});
      </pre> 
    </div> 
    <div class="site-title"> 
     <fieldset>
      <legend><a name="space">模块命名空间</a></legend>
     </fieldset> 
    </div> 
    <div class="site-text"> 
     <p>layui 的模块对象会绑定在 layui 对象下，内部由 layui.define() 方法来完成。每个模块都有一个特定命名，且无法被占用。所以你无需担心模块的空间被污染，除非你主动 delete layui['模块名']。调用模块可通过 layui.use() 来实现，再通过 layui 对象获得模块对象。如：</p> 
     <pre class="layui-code">layui.use(['layer', 'laypage', 'laydate'], function(){
  var layer = layui.layer //获得 layer 模块
  ,laypage = layui.laypage //获得 laypage 模块
  ,laydate = layui.laydate; //获得 laydate 模块
  
  //使用模块
});      
      </pre> 
     <p>我们推荐你将所有的业务代码都写在一个大的 use 回调中，而不是将模块接口暴露给全局，比如下面的方式我们是极不推荐的：</p> 
     <pre class="layui-code">//强烈不推荐下面的做法
var laypage, laydate;
layui.use(['laypage', 'laydate'], function(){
  laypage = layui.laypage;
  laydate = layui.laydate;
});
      </pre> 
     <p>你之所以想使用上面的错误方式，是想其它地方使用不在执行一次 layui.use？但这种理解本身是存在问题的。因为如果一旦你的业务代码是在模块加载完毕之前执行，你的全局对象将获取不到模块接口，因此这样用不仅不符合规范，还存在报错风险。建议在你的 js 文件中，在最外层写一个 layui.use 来加载所依赖的模块，并将业务代码写在回调中，这样还可以确保 html 文档加载完毕再执行回调代码。</p> 
    </div> 
    <div class="site-title"> 
     <fieldset>
      <legend><a name="extend">扩展一个 layui 模块</a></legend>
     </fieldset> 
    </div> 
    <div class="site-text"> 
     <p>layui 官方提供的模块有时可能还无法满足你，或者你试图按照layer的模块规范来扩展一个模块。那么你有必要认识<em>layui.define()</em>方法，相信你在文档左侧的“底层方法”中已有所阅读。下面就让我们一起扩展一个 layui 模块吧：</p> 
     <p>第一步：确认模块名，假设为：<em>mymod</em>，然后新建一个<em>mymod.js</em> 文件放入项目任意目录下（注意：不用放入layui目录）</p> 
     <p>第二步：编写 mymod.js 如下：</p> 
     <pre class="layui-code">/**
  扩展一个 mymod 模块
**/      
 
layui.define(function(exports){ //提示：模块也可以依赖其它模块，如：layui.define('mod1', callback);
  var obj = {
    hello: function(str){
      alert('Hello '+ (str||'mymod'));
    }
  };
  
  //输出 mymod 接口
  exports('mymod', obj);
});    
      </pre> 
     <p>第三步：设定扩展模块所在的目录，然后就可以在别的JS文件中使用了</p> 
     <pre class="layui-code">//config的设置是全局的
layui.config({
  base: '/res/js/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
  mymod: 'mymod' //如果 mymod.js 是在根目录，也可以不用设定别名
  ,mod1: 'admin/mod1' //相对于上述 base 目录的子目录
});
 
//你也可以忽略 base 设定的根目录，直接在 extend 指定路径（<span style="color: #FF5722;">主要：该功能为 layui 2.2.0 新增</span>）
layui.extend({
  mod2: '{/}http://cdn.xxx.com/lib/mod2' // {/}的意思即代表采用自有路径，即不跟随 base 路径
})
 
//使用拓展模块
layui.use(['mymod', 'mod1'], function(){
  var mymod = layui.mymod
  ,mod1 = layui.mod1
  ,mod2 = layui.mod2;
  
  mymod.hello('World!'); //弹出 Hello World!
});
      </pre> 
     <p>大体上来说，layui 的模块定义很类似 Require.js 和 Sea.js，但跟他们又有着明显的不同，譬如在接口输出等地方。</p> 
    </div> 
    <div class="site-title"> 
     <fieldset>
      <legend><a name="other">结语</a></legend>
     </fieldset> 
    </div> 
    <div class="site-text"> 
     <p>其实关于模块的核心，就是 layui.js 的两个底层方法：一个用于定义模块的 <em>layui.define()</em>，一个加载模块的 <em>layui.use()</em>。</p> 
    </div> 
    <div class="layui-elem-quote"> 
     <p>layui - 在每一个细节中，用心与你沟通</p> 
    </div> 
   </div> 
  </div> 
  <div class="layui-footer footer footer-doc"> 
   <p> Copyright © 2021 <a href="/index.html">layui.itmtr.cn</a> MIT Licensed</p> 
   <p> </p>
  </div> 
  <script>
window.global = {
  pageType: 'doc'
  ,preview: function(){
    var preview = document.getElementById('LAY_preview');
    return preview ? preview.innerHTML : '';
  }()
};
</script> 
  <div class="site-tree-mobile layui-hide"> <i class="layui-icon layui-icon-spread-left"></i> 
  </div> 
  <div class="site-mobile-shade"></div> 
  <script src="/layui/dist/layui.js" charset="utf-8"></script> 
  <script>
layui.config({
  base: '/static/lay/modules/layui/'
  ,version: '1632659138211'
}).use('global');
</script> 
  <script>
</script>  
 </body>
<script src="/common/common.js"></script>
</html>